package org.jradweb.core.taglib;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;

import org.jradweb.core.controller.AbstractMultiActionController;
import org.springframework.beans.BeansException;
/**
 * 
 *
 * Created On Apr 21, 2008
 * @author <a href="mailto:wagzhi@gmail.com">Paul Wang</a>
 * @version $Id: LinkTag.java 149 2008-10-10 03:10:10Z wagzhi $
 */
public class LinkTag extends UriTag{
	private String target;
	
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	@Override
	public int doEndTag() throws JspException {
		HttpServletRequest req=(HttpServletRequest)pageContext.getRequest();
		String contextPath=req.getContextPath();
		JspWriter writer =this.pageContext.getOut();
		String uri=this.getUri(contextPath);
		StringBuffer sb=new StringBuffer("<a href=\"").append(contextPath).append("/");
		BodyContent bc=this.getBodyContent();
		try {
			writer.write("<a ");
			
			if(target==null){
			writer.write("href=\"");
			writer.write(uri);
			writer.write("\">");
			}else{
				writer.write("href='#' onclick=\"javascript:j.ajax({type: 'GET',url: '");
				writer.write(uri);
				writer.write("', success: function(msg){j('#");
				writer.write(target);
				writer.write("').html(msg)}});\"> ");
			}
			if(bc!=null){
				writer.write(bc.getString());
			}
			writer.write("</a>");
			
			return EVAL_PAGE;
		} catch (BeansException e) {
			throw new JspException(e);
		} catch (IOException e) {
			throw new JspException(e);
		}
		finally{
			pageContext.removeAttribute(URI_PARAMETERS);
			pageContext.removeAttribute(URI_QURY_PARAMETERS);
		}
	}
}
